/*
 *   Clovis
 *   Copyright (C) 2008 SFWOL
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#include "Log.h"

namespace clovis {

	namespace io {

        Log::Log(){

        }

        void Log::logOn(ERROR_TYPE error , core::CString message , core::CString file ){

            time_t t;
            time(&t);
            core::CString detail;


            detail.clear();
            detail = ctime(&t);

			// Setting up the text to show on output
			switch(error){

				case FATAL_ERROR:
					detail = "FATAL ERROR :\n" + message.toStdString() + " " + detail.toStdString() + "\n";
					break;

				case ERROR:
					detail = "ERROR :\n" + message.toStdString() + " " + detail.toStdString() + "\n";
					break;

				case WARNING:
					detail = "WARNING :\n" + message.toStdString() + " " + detail.toStdString() + "\n";
					break;

			}

            // Print errors onto default output
            std::cerr << detail.toStdString();

            // Opening file to write the errors into
            std::ofstream temp( file.toCharArray() , std::ios_base::app );
            temp << detail.toStdString() << "\n";
            temp.close();


        }


        void Log::logOn(ERROR_TYPE error , core::CString message){
            core::CString file = "error.log";
            time_t t;
            time(&t);
            core::CString detail;


            detail.clear();
            detail = ctime(&t);

			// Setting up the text to show on output
			switch(error){

				case FATAL_ERROR:
					detail = "FATAL ERROR :\n" + message.toStdString() + " " + detail.toStdString() + "\n";
					break;

				case ERROR:
					detail = "ERROR :\n" + message.toStdString() + " " + detail.toStdString() + "\n";
					break;

				case WARNING:
					detail = "WARNING :\n" + message.toStdString() + " " + detail.toStdString() + "\n";
					break;

			}

            // Print errors onto default output
            std::cerr << detail.toStdString();

            // Opening file to write the errors into
            std::ofstream temp( file.toCharArray() , std::ios_base::app );
            temp << detail.toStdString() << "\n";
            temp.close();


        }


	};

};
